<?php
/* ----------------------------------------------------------
--               JacoCMS by Jaco Ruit                      --
-------------------------------------------------------------
--     JacoCMS is Open Source and is lincenced under       --
--             GNU General Public Licence v3               --
--          http://www.gnu.org/copyleft/gpl.html           --
-------------------------------------------------------------
--       Please don't remove any text from the footers.    --
-------------------------------------------------------------
-- Credits:   * Jaco Ruit          ~     Scripts,MySQL     --     
-----------------------------------------------------------*/

# JacoCMS Comment Class

class Comment
{
	public function getByPID($searchpid, $type)
	{
		if ($this->getCommentCount($searchpid, $type) == 0)
		{
			$htmlcode = "";
		}
		else
		{
			$htmlcode = '<div id="commentcount"><p style:"color:black;">' . $this->getCommentCount($searchpid, $type) . '</p>comments</div><br />';
		}
		$query = "SELECT id, poster, email, date, commenttext FROM comments WHERE pid='" . $searchpid . "' AND type='" . $type . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$poster = $row['poster'];
			$email = $row['email'];
			$date = $row['date'];
			$commenttext = $row['commenttext'];
			$arr = array("id" => $id, "poster" => $poster, "email" => $email, "date" => $date, "commenttext" => $commenttext);
			$htmlcode .= $this->getHTMLCode($arr);
		}
		mysql_free_result($result);
		$htmlcode .= '<a href=newcomment' . $searchpid . '-' . $type . '>Post comment..</a>';
		return $htmlcode;
	}
	
	public function getByID($searchid)
	{
		$query = "SELECT poster, email, date, commenttext, type, pid FROM comments WHERE id='" . $searchid . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$pid = $row['pid'];
			$poster = $row['poster'];
			$email = $row['email'];
			$date = $row['date'];
			$commenttext = $row['commenttext'];
			$type = $row['type'];
		}
		mysql_free_result($result);
		$arr = array("id" => $searchid, "pid" => $pid, "type" => $type, "poster" => $poster, "email" => $email, "date" => $date, "commenttext" => $commenttext);
		return $arr;
	}
	
	public function getTable($array)
	{
		$code = null;
		if ($array['type'] == 'v')
		{
			$code = '<tr><td>' . $array['id'] . '</td><td>' . $array['poster'] . '</td><td>Video</td><td>' . substr($array['commenttext'], 0, 10) . '..<td><a href="deletec-' . $array['id'] . '">Delete</a></td></tr>';
		}
		else if ($array['type'] == 'a')
		{
			$code = '<tr><td>' . $array['id'] . '</td><td>' . $array['poster'] . '</td><td>Article</td><td>' . substr($array['commenttext'], 0, 10) . '..<td><a href="deletec-' . $array['id'] . '">Delete</a></td></tr>';
		}
		return $code;
	}
	
	public function getWholeTable()
	{
		$htmlcode = "<table><tr><th>ID</th><th>Poster</th><th>Commented on</th><th>Comment</th></tr>";
		$query = "SELECT poster, type, id, commenttext FROM comments ORDER BY id DESC";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$poster = $row['poster'];
			$type = $row['type'];
			$commenttext = $row['commenttext'];
			$arr = array("id" => $id, "poster" => $poster, "type" => $type, "commenttext" => $commenttext);
			$htmlcode .= $this->getTable($arr);
		}
		$htmlcode .= '</table>';
		mysql_free_result($result);
		return $htmlcode;
	}
	
	public function delete($idtodel)
	{
		$query = "DELETE FROM comments WHERE id='" . $idtodel . "'";
		$result = mysql_query($query);
	}
	
	public function checkIfIDExists($idtosearch)
	{
		$query = "SELECT poster FROM comments WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			mysql_free_result($result);
			return true;
		}
		else
		{
			mysql_free_result($result);
			return false;
		}
	}
		
	public function rowCount()
	{
		$query = "SELECT id FROM comments";
		$result = mysql_query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
	
	public function getCommentCount($searchpid, $type)
	{
		$query = "SELECT poster FROM comments WHERE pid='" . $searchpid . "' AND type='" . $type . "'";
		$result = mysql_query($query);
		$count = mysql_num_rows($result);
		mysql_free_result($result);
		return $count;
	}
	
	public function post($commenttext, $poster, $email, $pid, $type)
	{
		$timestampsec = $_SERVER['REQUEST_TIME'];
		$datetime= date("Y-m-d H:i:s", $timestampsec);
		$idtopost = $this->getLastID() + 1; 
		$query = "INSERT INTO comments (id, pid, type, poster, email, date, commenttext) VALUES ('" . $idtopost . "','" . $pid . "','" . $type . "','" . $poster . "','" . $email ."','" . $datetime . "','" . $commenttext . "')";
		$result = mysql_query($query);
	}
		
	public function getHTMLCode($array)
	{
		return'<div id="comment">
			    <div id="commentinfo"><p>Posted on ' . $array['date'] . ' by ' . $array['poster'] . '</p></div>
				<p>' . $array['commenttext'] . '</p>
				</div><br />';
	}
	
	public function getLastID()
	{
		$query = "SELECT id FROM comments ORDER BY id DESC";
		$result = mysql_query($query);
		$lastid = null;
		$lastid = mysql_fetch_array($result);
		mysql_free_result($result);
		return $lastid['id'];
	}
}

?>